﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;


namespace Lib.CommonForm
{
    public class tbl_Users_Info : Standard_Info
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
        public bool isAdmin { get; set; }
        public bool viewAllCompany { get; set; }
        public bool viewAllBranch { get; set; }
        public bool IsAnonymous { get; set; }
        public int EmployeeId { get; set; }
    }
    public class tbl_Users_Length
    {
        public static int UserName = 256;
        public static int Password = 128;
    }
    public class tbl_Users_Controller
    {
        #region "Contructor"

        private static tbl_Users_Controller _instance;

        protected tbl_Users_Controller() { }

        protected void Dispose()
        {
            _instance = null;
        }

        private static object _syncLock = new object();

        public static tbl_Users_Controller Instance()
        {
            if (_instance == null)
            {
                lock (_syncLock)
                {
                    if (_instance == null)
                        _instance = new tbl_Users_Controller();
                }
            }
            return _instance;
        }

        #endregion

        public DataTable GetList(out int ItemCount, string orderBy, string orderDirection, int currentPage, int pageSize, string keyWord, int BranchID, int DepartmentID)
        {
            DataTable retVal = null;
            ItemCount = 0;
            DataSet ds = DataConnect.ExecuteDataset("prc_Users_GetList", orderBy, orderDirection, currentPage, pageSize, keyWord, BranchID, DepartmentID);
            if (ds != null)
            {
                retVal = ds.Tables[0];
                if (ds.Tables[1] != null)
                    ItemCount = Globals.ToInt32(ds.Tables[1].Rows[0][0]);
            }
            return retVal;
        }

        public DataSet GetAll()
        {
            return DataConnect.MainServiceWCF().aspnet_Users_GetAll();
        }
        public string[] GetUsersInRoles(string RoleName)
        {
            return DataConnect.MainServiceWCF().aspnet_Roles_GetUsersInRole(RoleName);
        }
        public DataTable GetUsersInRoles1(string RoleName)
        {
            return DataConnect.ExecuteDatatable("prc_Users_GetUsersInRoles", RoleName);
        }

        public bool Delete(string UserName)
        {

            DataConnect.ExecuteNonQuery("prc_Users_Delete", UserName);

            return DataConnect.MainServiceWCF().aspnet_Users_Delete(UserName);

        }



        public bool ChangePassword(string userName, string oldPass, string passWord)
        {
            return DataConnect.MainServiceWCF().aspnet_Users_ChangePassword(userName, oldPass, passWord); ;
        }

        public bool IsExist(string userName)
        {
            return DataConnect.MainServiceWCF().aspnet_Users_IsExist(userName); ;

        }

        public string Create(tbl_Users_Info i)
        {
            string retVal = "";
            retVal = DataConnect.MainServiceWCF().aspnet_Users_Create(i.UserName, i.Password, i.Email,i.isAdmin,i.viewAllCompany,i.viewAllBranch);

            if (retVal == "0")
                DataConnect.ExecuteNonQuery("prc_Users_Append", i.UserName, i.EmployeeId, FormGlobals.strUserName);

            return retVal;
        }


        public string Update(tbl_Users_Info i)
        {
            string retVal = "";
            retVal = DataConnect.MainServiceWCF().aspnet_Users_Update(i.UserName, i.Password, i.Email, i.isAdmin, i.viewAllCompany, i.viewAllBranch);

            if (retVal.Length == 0)
                DataConnect.ExecuteNonQuery("prc_Users_Append", i.UserName, i.EmployeeId, FormGlobals.strUserName);

            return retVal;
        }


        public tbl_Users_Info GetInfo(string userName)
        {
            tbl_Users_Info retVal = null;
            DataTable dt = null;
            dt = DataConnect.MainServiceWCF().aspnet_Users_GetInfo(userName).Tables[0];

            if (dt != null)
            {
                if (dt != null)
                    foreach (DataRow dr in dt.Rows)
                    {
                        retVal = new tbl_Users_Info();
                        retVal.FillDataProperty(dr);
                    }
            }

            dt = DataConnect.ExecuteDataset("prc_Users_GetInfo", userName).Tables[0];
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                    retVal.EmployeeId = Globals.ToInt32(dt.Rows[0]["EmployeeId"]);
            }
            return retVal;
        }
    }
}
